package com.icode.dao.impl;

import com.icode.core.model.house.*;
import com.icode.dao.HomePageDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-3-2
 * Time: 下午2:58
 * To change this template use File | Settings | File Templates.
 */
@Repository("homePageDao")
public class HomePageDaoImpl extends EntityDaoImpl implements HomePageDao {

    @Override
    public HomeIntroImage findHomeIntroImageByUploadImage(UploadImage uploadImage) {
        String hql = "from " + HomeIntroImage.class.getName() + " a where a.image=:image";
        Query query = currentSession().createQuery(hql);
        query.setParameter("image", uploadImage);
        return (HomeIntroImage) query.uniqueResult();
    }

    @Override
    public HomeInfoImage findHomeInfoImageByUploadImage(UploadImage uploadImage) {
        String hql = "from " + HomeInfoImage.class.getName() + " a where a.image=:image";
        Query query = currentSession().createQuery(hql);
        query.setParameter("image", uploadImage);
        return (HomeInfoImage) query.uniqueResult();
    }

    @Override
    public HomePartnerImage findHomePartnersByUploadImage(UploadImage uploadImage) {
        String hql = "from " + HomePartnerImage.class.getName() + " a where a.image=:image";
        Query query = currentSession().createQuery(hql);
        query.setParameter("image", uploadImage);
        return (HomePartnerImage) query.uniqueResult();
    }

    @Override
    public void deleteObject(Object object) {
        currentSession().delete(object);
    }

    @Override
    public List<HomeIntroImage> findHomeIntroImageByHomePageId(String id) {
        HomePage homePage = (HomePage) currentSession().get(HomePage.class, Integer.parseInt(id));
        String hql = "from " + HomeIntroImage.class.getName() + " a where a.homePage=:homePage";
        Query query = currentSession().createQuery(hql);
        query.setParameter("homePage", homePage);
        return query.list();
    }

    @Override
    public List<HomeInfoImage> findHomeInfoImageByHomePageId(String id) {
        HomePage homePage = (HomePage) currentSession().get(HomePage.class, Integer.parseInt(id));
        String hql = "from " + HomeInfoImage.class.getName() + " a where a.homePage=:homePage";
        Query query = currentSession().createQuery(hql);
        query.setParameter("homePage", homePage);
        return query.list();
    }

    @Override
    public List<HomePartnerImage> findHomePartnersImageByHomePageId(String id) {
        HomePage homePage = (HomePage) currentSession().get(HomePage.class, Integer.parseInt(id));
        String hql = "from " + HomePartnerImage.class.getName() + " a where a.homePage=:homePage";
        Query query = currentSession().createQuery(hql);
        query.setParameter("homePage", homePage);
        return query.list();
    }

    @Override
    public void deleteIntroImages(List<HomeIntroImage> homeIntroImages) {
        for (HomeIntroImage h : homeIntroImages) {
            currentSession().delete(h);
        }
    }

    @Override
    public void deleteInfoImages(List<HomeInfoImage> homeInfoImages) {
        for (HomeInfoImage h : homeInfoImages) {
            currentSession().delete(h);
        }
    }

    @Override
    public void deletePartnersImages(List<HomePartnerImage> homePartnerImages) {
        for (HomePartnerImage h : homePartnerImages) {
            currentSession().delete(h);
        }
    }

    @Override
    public HomePage findHomePage() {
        String hql = "from " + HomePage.class.getName();
        Query query = currentSession().createQuery(hql);
        List<HomePage> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }
}
